/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Deal.PersonalInformation.View;

import Deal.ItemsCreater.Items_Book;
import Deal.ViewController.ViewController;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;


/**
 *
 * @author LiuBole
 */
public class CollectionJPanel extends javax.swing.JPanel {

    private ViewController viewController;
    private DefaultTableModel bookTable = new DefaultTableModel(null, Items_Book.COLLECTION_COLUMN_NAMES);
    /**
     * Creates new form CollectionJPanel
     */
    public CollectionJPanel(ViewController controller) {
        initComponents();
        viewController = controller;
        initData();
    }
    private void initData(){
        this.bookTable.setDataVector(viewController.collectionTableVector(), Items_Book.COLLECTION_COLUMN_NAMES);
        jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable(){
            public boolean isCellEditable(int row, int column){
                return false;
            }
        };
        jButton2 = new javax.swing.JButton();

        jTable1.setModel(bookTable);
        jScrollPane1.setViewportView(jTable1);

        jButton2.setText("移除");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 337, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton2)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    public Object[] getSelectedObject(){
        Object[] book = null;
        int row = jTable1.getSelectedRow();
        if(row!=-1){
            book = new Object[jTable1.getColumnCount()]; //记录选中的书籍信息
            for(int i=0; i<jTable1.getColumnCount(); i++){
                book[i] = jTable1.getValueAt(row, i);
            }
        } else{
            JOptionPane.showMessageDialog(null, "请选择一个条目!");
        }
        return book;
    }
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        int index = jTable1.getSelectedRow();
        Object book[] = getSelectedObject();
        if(book!=null){
            if(viewController.removeFromCollection(String.valueOf(book[0]))){
                JOptionPane.showMessageDialog(this, "移除成功！");
                DefaultTableModel tableModel = (DefaultTableModel)jTable1.getModel(); 
                tableModel.removeRow(index);
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
